"""
 Decision tree for classification. A decision tree can be learned by
 splitting the training set into subsets based on an attribute value
 test. This process is repeated on each derived subset in a recursive
 manner called recursive partitioning. The recursion is completed when
 the subset at a node all has the same value of the target variable,
 or when splitting no longer adds value to the predictions.
"""

from smile.data.parser import ArffParser,IOUtils
from smile.classification import  DecisionTree
from smile.math import Math
from smile.validation import LOOCV
from jhplot import Web
from java.io import File
from jarray import zeros,array
import java


# this function extract data[][] and label[] array from datasets
def getJavaArrays(dataset):
    rows=dataset.size()
    lst = [0.0]*rows
    twoDimArr = array([lst,[]], java.lang.Class.forName('[D'))
    data = dataset.toArray(twoDimArr)
    label = dataset.toArray(zeros(rows, "i"))
    return data,label

http="http://datamelt.org/examples/data/weka/"
print "Reading data from",http
datasource="weather.nominal.arff"  
datasetName="Weka"
print Web.get(http+datasource)

arffParser = ArffParser()
arffParser.setResponseIndex(4)
weather=arffParser.parse(File(datasource))
print weather 
print "arff  training for classification.."

x,y=getJavaArrays(weather)
n=len(y)
loocv = LOOCV(n)
error = 0

for i in range(n):
        trainx = Math.slice(x, loocv.train[i]);
        trainy = Math.slice(y, loocv.train[i]);
        tree = DecisionTree(weather.attributes(),trainx,trainy,3);
        org=y[loocv.test[i]]
        pred=tree.predict(x[loocv.test[i]])
        if (org != pred): error +=1;
        print org, " predicted=",pred
print "Decision Tree error = ", error


